package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.ProduceinfoMapper;
import com.lucher.mes.dao.ProduceproductMapper;
import com.lucher.mes.dao.ProducescheduleMapper;
import com.lucher.mes.dao.ProducescheduleproblemMapper;
import com.lucher.mes.dao.ProductMapper;
import com.lucher.mes.dao.WorklineMapper;
import com.lucher.mes.po.Produceinfo;
import com.lucher.mes.po.Produceschedule;
import com.lucher.mes.po.Producescheduleproblem;
import com.lucher.mes.po.Workline;

@Controller
@RequestMapping("/screanshow")
public class ScreanshowController extends BaseController{
	
	@Autowired
	private WorklineMapper worklineDao;
	@Autowired
	private ProduceproductMapper produceproductDao;
	@Autowired
	private ProducescheduleproblemMapper producesheduleproblemDao;
	@Autowired
	private ProduceinfoMapper produceinfoDao;
	@Autowired
	private ProducescheduleMapper producesheduleDao;
	/**
	 * 查询大屏显示
	 */
	@RequestMapping(value = "/findBigscreanshow")
	public String findBigscreanshow(Model moel) {
		return "screanshow/findBigscreanshow";
	}
	
	/**
	 * 查询大屏显示数据Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findBigscreanshowData")
	public Map<String, Object> findBigscreanshowData(Workline record,Producescheduleproblem problemRecord) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		List<Workline> worklineList = worklineDao.findByList(record);
		for (Workline workline : worklineList) {
			HashMap<String, Object> worklineMap = new HashMap<String, Object>();
			//产线编号
			worklineMap.put("linecode", workline.getCode());
			//产线名称
			worklineMap.put("linename", workline.getName());
			
			//查询产线当前上线的生产任务
			Produceinfo produceinfo = produceinfoDao.findByLinecode(workline.getCode());
			if(produceinfo == null){
				produceinfo = new Produceinfo();
			}
			//实时产量
			Integer completedCount = produceproductDao.findCompletedCountByLinecode(workline.getCode());
			if(completedCount == null){
				completedCount= 0;
			}
			worklineMap.put("producedCount", completedCount);
			//合格产量
			//Integer qualifiedCount = produceproductDao.findQualifiedCountByProducecode(produceinfo.getProducecode());
			//worklineMap.put("qualifiedCount", qualifiedCount);
			//合格率
			//if(producedCount == null || producedCount == 0){
			//	worklineMap.put("qualitiedRate", Tools.getPercentage(0));
			//}else{
			//	worklineMap.put("qualitiedRate", Tools.getPercentage((float)qualifiedCount/(float)producedCount));
			//}
			//问题总数
			problemRecord.setLinecode(workline.getCode());
			problemRecord.setProducecode(produceinfo.getProducecode());
			int problemCount = producesheduleproblemDao.findByCount(problemRecord);
			worklineMap.put("problemCount", problemCount);
			objectlist.add(worklineMap);
		}
		result.put("data", objectlist);
		return result;
	}
	
	/**
	 * 查询所有产线
	 */
	@RequestMapping(value = "/findWorklineListForSmallscreanshow")
	public String findWorklineForSmallscreanshow(Model moel) {
		return "screanshow/findWorklineListForSmallscreanshow";
	}
	
	
	/**
	 * 查询小屏显示
	 */
	@RequestMapping(value = "/findSmallscreanshow")
	public String findSmallscreanshow(Model model,Workline record) {
		record = worklineDao.findByCode(record.getCode());
		if(record == null){
			record = new Workline();
		}
		model.addAttribute(record);
		//当前产线正在上线的任务
		Produceinfo produceinfo = produceinfoDao.findByLinecode(record.getCode());
		if(produceinfo == null){
			produceinfo = new Produceinfo();
		}
		model.addAttribute(produceinfo);
		
		return "screanshow/findSmallscreanshow";
	}
	
	/**
	 * 查询小屏数据Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findSmallscreanshowData")
	public Map<String, Object> findSmallscreanshowData(Produceinfo record,Produceschedule scheduleRecord,Producescheduleproblem problemRecord) {
		
		String linecode = getRequest().getParameter("linecode");
		
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		record = produceinfoDao.findByLinecode(record.getLinecode());
		if(record != null){
			//当前任务的实时产量
			Integer producedCount = produceproductDao.findCompletedCountByLinecode(linecode);
			if(producedCount == null){
				producedCount= 0;
			}
			
			scheduleRecord.setProducecode(record.getProducecode());
			List<Produceschedule> producescheduleList =  producesheduleDao.findProducedCountByProducecode(scheduleRecord);
			
			for (Produceschedule produceschedule : producescheduleList) {
				HashMap<String, Object> objectMap = new HashMap<String, Object>();
				objectMap.put("prisonername", produceschedule.getPrisonername()); //员工姓名
				objectMap.put("producedCount", producedCount);                    //实时产量
				objectMap.put("qualifiednum", produceschedule.getDepotamount());  //合格产量
				objectMap.put("plannum", record.getAmount());                     //计划产量
				//完成率
				if(record.getAmount() == null || record.getAmount() == 0){
					objectMap.put("producedRate", Tools.getPercentage(0));
				}else{
					objectMap.put("producedRate", Tools.getPercentage((float)producedCount/(float)record.getAmount()));
				}
				//合格率
				if(producedCount == null || producedCount == 0){
					objectMap.put("qualitiedRate", Tools.getPercentage(0));
				}else{
					objectMap.put("qualitiedRate", Tools.getPercentage((float)produceschedule.getDepotamount()/(float)producedCount));
				}
				//问题总数
				problemRecord.setLinecode(record.getLinecode());
				problemRecord.setProducecode(record.getProducecode());
				problemRecord.setPrisonercode(produceschedule.getPrisonercode());
				int problemCount = producesheduleproblemDao.findByCount(problemRecord);
				objectMap.put("problemCount", problemCount);
				
				objectlist.add(objectMap);
			}
			result.put("count", producescheduleList.size());
			result.put("data", objectlist);
		}
		
		return result;
	}

	
}
